<template>
  <div>
    <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{artObj.title}}</span>
          <!-- 图片懒加载 -->
          <lazy-component>
            <!-- 单图 -->
            <img v-if="artObj.cover.type === 1" class="thumb" v-lazy="artObj.cover.images[0]" />
          </lazy-component>
        </div>
        <!-- 三图 -->
        <div class="thumb-box" v-if="artObj.cover.type > 1">
          <!-- 图片懒加载 -->
          <lazy-component>
            <img v-for="(imgUrl, index) in artObj.cover.images" :key="index" class="thumb" v-lazy="imgUrl" />
          </lazy-component>
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{artObj.aut_name}}</span>
            <span>{{artObj.comm_count}}评论</span>
            <span>{{artObj.pubdate}}</span>
          </div>
          <!-- 反馈按钮 会有冒泡导致跳转到文章详情页 加stop -->
          <van-icon name="cross" @click.stop="show = true" v-if="isShow" />
        </div>
      </template>
    </van-cell>
    <!-- 反馈框 指定body为挂载点功能才正常 -->
    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" get-container="body" :cancel-text="bottomText" @cancel="cancelFn" @close="closeFn" />
  </div>
</template>

<script>
import { timeAgo } from '@/utils/timeFormat.js'
import { firstActions, secondActions } from '@/api/report.js'
import { Toast } from 'vant'
export default {
  data() {
    return {
      show: false,
      actions: firstActions,
      bottomText: '取消' // 底部选项卡的默认文字
    }
  },
  props: {
    artObj: Object,
    isShow: {
      type: Boolean,
      default: true // 默认为true(不需要再去首页里传了)
    }
  },
  methods: {
    timeFormat: timeAgo,
    onSelect(action, index) { // action: {name: '其它问题', value: 0}
      if (action.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.bottomText = '返回'
      } else if (action.name === '不感兴趣') {
        // 把不感兴趣的文章ID传给父组件
        this.$emit('dislikeEV', this.artObj.art_id)
        // 反馈成功后 关闭反馈框
        this.show = false
        // Toast('反馈成功')
      } else { // 二级的所有选项卡
        this.$emit('reportEv', this.artObj.art_id, action.value)
        // 举报成功后关闭反馈框
        this.show = false
      }
    },
    // 反馈框底部按钮
    cancelFn() {
      if (this.bottomText === '返回') {
        this.show = true
        this.actions = firstActions
        this.bottomText = '取消'
        return
      }
      Toast('取消')
    },
    // 反馈面板关闭时触发
    closeFn() {
      this.actions = firstActions
      this.bottomText = '取消'
    }

  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between; // 主轴排列方式先两边对齐再平分中间空间
}
</style>
